package com.d3works.apps.bankpayment.web;

import com.d3works.apps.bankpayment.db.BankPaymentBatch;
import com.d3works.apps.bankpayment.db.repo.BankPaymentBatchRepository;
import com.d3works.apps.bankpayment.service.batch.BankPaymentBatchService;
import com.d3works.apps.base.callable.ControllerCallableTool;
import com.d3works.apps.base.db.Batch;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;

public class BankPaymentBatchController {

  public void runBatch(ActionRequest request, ActionResponse response) {
    try {
      BankPaymentBatch bankPaymentBatch = request.getContext().asType(BankPaymentBatch.class);
      BankPaymentBatchService bankPaymentBatchService = Beans.get(BankPaymentBatchService.class);
      bankPaymentBatchService.setBatchModel(
          Beans.get(BankPaymentBatchRepository.class).find(bankPaymentBatch.getId()));
      ControllerCallableTool<Batch> controllerCallableTool = new ControllerCallableTool<>();

      Batch batch = controllerCallableTool.runInSeparateThread(bankPaymentBatchService, response);

      if (batch != null) {
        response.setInfo(batch.getComments());
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    } finally {
      response.setReload(true);
    }
  }
}
